/**
 * 
 */
package com.xbang.attendance.web.action.warehouse.stock;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.product.ProductCategory;
import com.xbang.attendance.entity.stock.Inventory;
import com.xbang.attendance.entity.system.Region;
import com.xbang.attendance.entity.system.Warehouse;
import com.xbang.attendance.service.InventoryService;
import com.xbang.attendance.service.ProductService;
import com.xbang.attendance.service.RegionService;
import com.xbang.attendance.service.WarehouseService;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.dao.search.component.SearchComponent;
import com.xbang.core.page.Result;
import com.xbang.core.struts.support.SearchActionSupport;

/**
 * @author dev.zhang
 * 
 */
public class Search extends SearchActionSupport {

	private static final long serialVersionUID = 8792923545485296635L;

	private static final Log logger = LogFactory.getLog(Search.class);

	@Autowired
	private ProductService productService;

	@Autowired
	private InventoryService inventoryService;
	
	@Autowired 
	private RegionService regionService;
	@Autowired
	private WarehouseService warehouseService;

	private List<Warehouse> warehouses = new ArrayList<Warehouse>();

	/** 排序 */
	private String firstOrder, secondOrder, thirdOrder, firstOrderSort, secondOrderSort, thirdOrderSort;

	/** 最终查询出来的库存记录 */
	private List<Inventory> inventorys = new ArrayList<Inventory>();

	/** 供选择的地区 */
	private List<Region> regions = new ArrayList<Region>();

	/** 产品类别 */
	private List<ProductCategory> sroductCategorys = new ArrayList<ProductCategory>();

	/** 第一次查询的仓库 */
	private Warehouse warehouse;
	
	

	private String number;
	private String code;
	
	private Region region;
	
	public String execute() throws Exception {

		String parameterKey = "eqInventoryWarehouseCode";
		String wereSentenceKey = "eq.inventory.warehouse.code";
		searchComponent.resetWhereSentence();
		extractSearchComponent(parameterKey, wereSentenceKey);

		regions = regionService.getAKZRegion();

		sroductCategorys = inventoryService.getObjects(ProductCategory.class);

		return "search";
	}

	@Action("/warehouse/stock/order/do")
	public String doOrder() throws Exception {

		String hqlSentence = "FROM Warehouse warehouse WHERE 1 = 1 ";
		String parameterKey = "eqInventoryWarehouseCode";

		Object value = getSearchParameterValue(parameterKey);
		if (value != null) {
			SearchComponent tmpSearchComponent = new SearchComponent();
			tmpSearchComponent.getParameters().put("eqWarehouseCode", value);
			tmpSearchComponent.getWhereSentences().put("eq.warehouse.code", " AND (warehouse.code = :eqWarehouseCode)");

			List objects = inventoryService.searchForList(hqlSentence, tmpSearchComponent);
			if (objects != null && !objects.isEmpty()) {
				warehouse = (Warehouse) objects.get(0);
			} else {
				return "r:/warehouse/stock/list/search";
			}
		} else {
			return "r:/warehouse/stock/list/search";
		}

		hqlSentence = "FROM Inventory inventory WHERE 1 = 1 ";

		searchComponent.resetOrderSentence();

		if (!StringUtils.isEmpty(firstOrder)) {
			searchComponent.addSearchElement(firstOrder, firstOrderSort);
		}
		if (!StringUtils.isEmpty(secondOrder)) {
			searchComponent.addSearchElement(secondOrder, secondOrderSort);
		}
		if (!StringUtils.isEmpty(thirdOrder)) {
			searchComponent.addSearchElement(thirdOrder, thirdOrderSort);
		}
		Result result = inventoryService.searchByPage(hqlSentence, searchComponent, page);
		inventorys = result.getItems();
		page = result.getPage();

		return "index";
	}

	@Action("/warehouse/stock/list")
	public String list() throws Exception {

		String hqlSentence = "FROM Warehouse warehouse WHERE 1 = 1 ";
		String parameterCodeKey = "eqInventoryWarehouseCode";
		String parameterIdKey = "eqInventoryWarehouseId";

		Object codeValue = getSearchParameterValue(parameterCodeKey);
		Object idValue = getSearchParameterValue(parameterIdKey);
		if (idValue != null || codeValue != null) {
			SearchComponent tmpSearchComponent = new SearchComponent();
			if (idValue != null) {
				tmpSearchComponent.addSearchElement("eq.warehouse.id", idValue);
			} else if (codeValue != null) {
				tmpSearchComponent.addSearchElement("eq.warehouse.code", codeValue);
			}

			List objects = inventoryService.searchForList(hqlSentence, tmpSearchComponent);
			if (objects != null && !objects.isEmpty()) {
				warehouse = (Warehouse) objects.get(0);
			} else {
				return "r:/warehouse/stock/list/search";
			}
		} else {
			return "r:/warehouse/stock/list/search";
		}

		hqlSentence = "FROM Inventory inventory WHERE 1 = 1 ";
		Result result = inventoryService.searchByPage(hqlSentence, searchComponent, page);
		inventorys = result.getItems();
		page = result.getPage();

		return "index";

	}

	/**
	 * @return
	 * @throws Exception
	 */
	@Action("/warehouse/stock/search/do")
	public String doSearch() throws Exception {

		String hqlSentence = "FROM Warehouse warehouse WHERE 1 = 1 ";
		String parameterKey = "eqInventoryWarehouseCode";

		Object value = getSearchParameterValue(parameterKey);
		if (value != null) {
			SearchComponent tmpSearchComponent = new SearchComponent();
			tmpSearchComponent.getParameters().put("eqWarehouseCode", value);
			tmpSearchComponent.getWhereSentences().put("eq.warehouse.code", " AND (warehouse.code = :eqWarehouseCode)");

			List objects = inventoryService.searchForList(hqlSentence, tmpSearchComponent);
			if (objects != null && !objects.isEmpty()) {
				warehouse = (Warehouse) objects.get(0);
			} else {
				return "r:/warehouse/stock/list/search";
			}
		} else {
			return "r:/warehouse/stock/list/search";
		}

		warehouses = inventoryService.getObjects(Warehouse.class);
		hqlSentence = "FROM Inventory inventory WHERE 1 = 1 ";

		Result result = inventoryService.searchByPage(hqlSentence, searchComponent, page);
		inventorys = result.getItems();
		page = result.getPage();

		return "index";
	}
	
	@Action("/warehouse/stock/search/filter")
	public String searchFilter()  throws Exception  {
		
		regions = regionService.getAKZRegion();
		
		return "stock-list-search-filter";
	}
	
	@Action("/warehouse/stock/search/filter/do")
	public String doSearchFilter()  throws Exception  {
		Map WhereSentencesMap = searchComponent.getWhereSentences();
		String hqlSentence = "FROM Warehouse warehouse WHERE 1 = 1 ";
		
		SearchComponent tmpSearchComponent = new SearchComponent();
		List objects = null;
		
		if( StringUtils.isEmpty(code) && StringUtils.isNotEmpty(number))
		{
			warehouses = warehouseService.getWarehousesByRegion(number);
			return "list-search";
		}
		Map map = new HashMap();
		if(StringUtils.isNotEmpty(code)){
			hqlSentence += " and warehouse.code = :code ";
			map.put("code", code);
		}
		if(StringUtils.isNotEmpty(number)){
			hqlSentence += " and warehouse.region.number = :number";
			map.put("number", number);
		}
		 objects = inventoryService.searchForList(hqlSentence, map);
		 
		if (objects != null && !objects.isEmpty()) {
			warehouse = (Warehouse) objects.get(0);
			hqlSentence = "FROM Inventory inventory WHERE 1 = 1 ";
			tmpSearchComponent = new SearchComponent();
			tmpSearchComponent.addSearchElement("eq.inventory.warehouse.id", warehouse.getId());
			Result result = inventoryService.searchByPage(hqlSentence, tmpSearchComponent, page);
			inventorys = result.getItems();
			page = result.getPage();
			return "index";
		} else {
			return "list-search";
		}
	}

	@Override
	public void resetCustomerSearchCriteria() {
		// TODO Auto-generated method stub

	}

	public ProductService getProductService() {
		return productService;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public InventoryService getInventoryService() {
		return inventoryService;
	}

	public void setInventoryService(InventoryService inventoryService) {
		this.inventoryService = inventoryService;
	}

	public List<Inventory> getInventorys() {
		return inventorys;
	}

	public void setInventorys(List<Inventory> inventorys) {
		this.inventorys = inventorys;
	}

	public List<ProductCategory> getSroductCategorys() {
		return sroductCategorys;
	}

	public void setSroductCategorys(List<ProductCategory> sroductCategorys) {
		this.sroductCategorys = sroductCategorys;
	}

	/**
	 * @return the regions
	 */
	public List<Region> getRegions() {
		return regions;
	}

	/**
	 * @param regions
	 *            the regions to set
	 */
	public void setRegions(List<Region> regions) {
		this.regions = regions;
	}

	/**
	 * @return the warehouse
	 */
	public Warehouse getWarehouse() {
		return warehouse;
	}

	/**
	 * @param warehouse
	 *            the warehouse to set
	 */
	public void setWarehouse(Warehouse warehouse) {
		this.warehouse = warehouse;
	}

	/**
	 * @return the firstOrder
	 */
	public String getFirstOrder() {
		return firstOrder;
	}

	/**
	 * @param firstOrder
	 *            the firstOrder to set
	 */
	public void setFirstOrder(String firstOrder) {
		this.firstOrder = firstOrder;
	}

	/**
	 * @return the secondOrder
	 */
	public String getSecondOrder() {
		return secondOrder;
	}

	/**
	 * @param secondOrder
	 *            the secondOrder to set
	 */
	public void setSecondOrder(String secondOrder) {
		this.secondOrder = secondOrder;
	}

	/**
	 * @return the thirdOrder
	 */
	public String getThirdOrder() {
		return thirdOrder;
	}

	/**
	 * @param thirdOrder
	 *            the thirdOrder to set
	 */
	public void setThirdOrder(String thirdOrder) {
		this.thirdOrder = thirdOrder;
	}

	/**
	 * @return the firstOrderSort
	 */
	public String getFirstOrderSort() {
		return firstOrderSort;
	}

	/**
	 * @param firstOrderSort
	 *            the firstOrderSort to set
	 */
	public void setFirstOrderSort(String firstOrderSort) {
		this.firstOrderSort = firstOrderSort;
	}

	/**
	 * @return the secondOrderSort
	 */
	public String getSecondOrderSort() {
		return secondOrderSort;
	}

	/**
	 * @param secondOrderSort
	 *            the secondOrderSort to set
	 */
	public void setSecondOrderSort(String secondOrderSort) {
		this.secondOrderSort = secondOrderSort;
	}

	/**
	 * @return the thirdOrderSort
	 */
	public String getThirdOrderSort() {
		return thirdOrderSort;
	}

	/**
	 * @param thirdOrderSort
	 *            the thirdOrderSort to set
	 */
	public void setThirdOrderSort(String thirdOrderSort) {
		this.thirdOrderSort = thirdOrderSort;
	}

	/**
	 * @return the warehouses
	 */
	public List<Warehouse> getWarehouses() {
		return warehouses;
	}

	/**
	 * @param warehouses
	 *            the warehouses to set
	 */
	public void setWarehouses(List<Warehouse> warehouses) {
		this.warehouses = warehouses;
	}

	public Region getRegion() {
		return region;
	}

	public void setRegion(Region region) {
		this.region = region;
	}
	

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

}
